package grape.attend.rest.overtime.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.attend.rest.overtime.form.OvertimeLegalListForm;
import grape.attend.service.overtime.query.OvertimeLegalListQuery;
import grape.attend.service.overtime.query.OvertimeLegalWithNullCondition;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.attend.rest.overtime.form.OvertimeLegalCreateForm;
import grape.attend.rest.overtime.form.OvertimeLegalUpdateForm;
import grape.attend.rest.overtime.form.OvertimeLegalListPageForm;
import grape.attend.rest.overtime.vo.OvertimeLegalVo;
import grape.attend.rest.overtime.mapper.OvertimeLegalWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.attend.service.overtime.po.OvertimeLegal;
import grape.attend.service.overtime.api.IOvertimeLegalService;
import java.util.List;
/**
 * <p>
 * 加班法定节假日设置表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/attend/overtimelegal")
@Api(tags = "加班法定节假日设置相关接口")
public class OvertimeLegalController extends BaseController<OvertimeLegalVo, OvertimeLegal> {

    @Autowired
    private OvertimeLegalWebMapper currentWebMapper;
    @Autowired
    private IOvertimeLegalService currentService;



     @ApiOperation("添加加班法定节假日设置")
     @PreAuthorize("hasAuthority('overtimelegal:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public OvertimeLegalVo create(@RequestBody @Valid OvertimeLegalCreateForm cf) {
         OvertimeLegal po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询加班法定节假日设置")
     @PreAuthorize("hasAuthority('overtimelegal:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public OvertimeLegalVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除加班法定节假日设置")
     @PreAuthorize("hasAuthority('overtimelegal:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新加班法定节假日设置")
     @PreAuthorize("hasAuthority('overtimelegal:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public OvertimeLegalVo update(@PathVariable String id,@RequestBody @Valid OvertimeLegalUpdateForm uf) {
         OvertimeLegal po = currentWebMapper.formToPo(uf);
         po.setId(id);
         if(!po.getIsAllowOvertime()){
             OvertimeLegalWithNullCondition overtimeLegalWithNullCondition = new OvertimeLegalWithNullCondition();
             overtimeLegalWithNullCondition.setIsApprove(true);
             overtimeLegalWithNullCondition.setIsDeductRestTime(true);
             overtimeLegalWithNullCondition.setRestTime(true);
             overtimeLegalWithNullCondition.setPo(po);
             return super.update(overtimeLegalWithNullCondition);
         }
         if(!po.getIsDeductRestTime()){
             OvertimeLegalWithNullCondition overtimeLegalWithNullCondition = new OvertimeLegalWithNullCondition();
             overtimeLegalWithNullCondition.setRestTime(true);
             overtimeLegalWithNullCondition.setPo(po);
             return super.update(overtimeLegalWithNullCondition);
         }
         return super.update(po);
     }
    @ApiOperation("不分页查询加班法定节假日设置")
    @PreAuthorize("hasAuthority('overtimelegal:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<OvertimeLegalVo> list(OvertimeLegalListForm listForm) {
        OvertimeLegal po = currentWebMapper.formToPo(listForm);
        OvertimeLegalListQuery overtimeLegalListQuery = new OvertimeLegalListQuery();
        overtimeLegalListQuery.setPoQuery(po);
        return super.list(overtimeLegalListQuery);
    }
    @ApiOperation("分页查询加班法定节假日设置")
    @PreAuthorize("hasAuthority('overtimelegal:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<OvertimeLegalVo> listPage(OvertimeLegalListPageForm listPageForm) {
         OvertimeLegal po = currentWebMapper.formToPo(listPageForm);
        OvertimeLegalListQuery overtimeLegalListQuery = new OvertimeLegalListQuery();
        overtimeLegalListQuery.setPoQuery(po);
        return super.listPage(overtimeLegalListQuery,listPageForm);
     }

}
